package com.jnsw.testkotlin.utils;

import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.jnsw.testkotlin.MyApplication;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.Map;

import cz.msebera.android.httpclient.Header;


/**
 * Created by Administrator on 2017/11/17 0017.
 */

public class FileUtil {

    public static void uploadFile(String path, String url) throws Exception {
        final File file = new File(path);
//        EventBus.getDefault().register(MyApplication.Companion.getInstance());
        if (file.exists() && file.length() > 0) {
            AsyncHttpClient client = new AsyncHttpClient();
            RequestParams params = new RequestParams();
            params.put("uploadfile", file);
            params.put("filename", "SSS.gif");
            params.put("id", "1245");
            params.put("guid", "ajhiugabeasdlkajsd");
            // 上传文件
            client.post(url, params, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers,
                                      byte[] responseBody) {
                    // 上传成功后要做的工作
//                    Toast.makeText(mContext, "上传成功", Toast.LENGTH_LONG).show();
///                    progress.setProgress(0);
                    String  response = new String(responseBody) ;
                    JSONObject object = JSONObject.parseObject(response);
//                    String msg = object.getString("msg");
//                    String path = object.getString("path");
//                    String name = object.getString("filename");
                  Map<String,Object>map =  object.getInnerMap();
                    String msg = map.get("msg").toString();
                    String path = map.get("path").toString();
                    String name = map.get("filename").toString();

                    Tip.Long(MyApplication.Companion.getInstance(),msg+":"+name +"---"+path);
                }

                @Override
                public void onFailure(int statusCode, Header[] headers,
                                      byte[] responseBody, Throwable error) {
                    // 上传失败后要做到工作
//                    Toast.makeText(mContext, "上传失败", Toast.LENGTH_LONG).show();
                    Tip.Long(MyApplication.Companion.getInstance(),"上传失败");
                }

                @Override
                public void onProgress(long bytesWritten, long totalSize) {
                    // TODO Auto-generated method stub
                    super.onProgress(bytesWritten, totalSize);
                    int count = (int) ((bytesWritten * 1.0 / totalSize) * 100);
                    // 上传进度显示
//                    progress.setProgress(count);
                    EventBus.getDefault().postSticky(count);
                    Log.e("上传 Progress>>>>>", bytesWritten + " / " + totalSize);
                }

                @Override
                public void onRetry(int retryNo) {
                    // TODO Auto-generated method stub
                    super.onRetry(retryNo);
                    // 返回重试次数
                }

            });
        } else {
//            Toast.makeText(mContext, "文件不存在", Toast.LENGTH_LONG).show();
            Tip.Long(MyApplication.Companion.getInstance(),"文件不存在");
        }
    }






}
